package com.fts.admin.handle;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 自定义元数据对象处理器
 * @author lzt
 * @date 2023/11/21 22:10
 */
@Component
@Slf4j
public class MyMetaObjectHandle implements MetaObjectHandler {

    /**
     * 插入操作自动填充
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        if(ObjectUtils.isEmpty(metaObject.getValue("createTime"))){
            metaObject.setValue("createTime", LocalDateTime.now());
        }
        if(ObjectUtils.isEmpty(metaObject.getValue("updateTime"))){
            metaObject.setValue("updateTime", LocalDateTime.now());
        }
        if(ObjectUtils.isEmpty(metaObject.getValue("createUser"))){
            metaObject.setValue("createUser", "admin");
        }
        if(ObjectUtils.isEmpty(metaObject.getValue("updateUser"))){
            metaObject.setValue("updateUser", "admin");
        }
    }

    /**
     * 更新操作自动填充
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        if(ObjectUtils.isEmpty(metaObject.getValue("createUser"))){
            metaObject.setValue("createUser", "admin");
        }
        if(ObjectUtils.isEmpty(metaObject.getValue("updateUser"))){
            metaObject.setValue("updateUser", "admin");
        }
    }
}
